package com.tang.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.tang.entity.Student;
import com.tang.service.StudentService;
import com.tang.service.impl.StudentServiceImpl;
import com.tang.utils.DateUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Date;

/**
 * @Author TangBowen
 * @create 2021/6/7 13:45
 */
@WebServlet("/studentServlet")
public class StudentController extends HttpServlet {

    StudentService studentService = new StudentServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");

        //获取方法的名字
        String method = req.getParameter("method");

        if (method.equals("selectByPage")) {
            //分页查询学生功能
            selectByPage(req, resp);
        } else if (method.equals("addStu")) {
            //添加数据功能
            addStu(req, resp);
        } else if (method.equals("updateStu")) {
            updateStu(req, resp);
        }else if (method.equals("deleteStu")) {
            deleteStu(req, resp);
        }

    }
    /**
     * 删除学生的方法
     */
    private void deleteStu(HttpServletRequest req, HttpServletResponse resp) {
        String number = req.getParameter("number");
        String currentPage = req.getParameter("currentPage");
        String pageSize = req.getParameter("pageSize");
        studentService.deleteStu(number);
        //重定向到分页查询功能
        try {
            resp.sendRedirect(req.getContextPath() + "/studentServlet?method=selectByPage&currentPage=" + currentPage + "&pageSize=" + pageSize);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * 修改学生方法。
     *
     * @param req
     * @param resp
     */
    private void updateStu(HttpServletRequest req, HttpServletResponse resp) {
        String number = req.getParameter("number");
        String name = req.getParameter("name");
        String address = req.getParameter("address");
        String birthday = req.getParameter("birthday");
        Date date = DateUtil.StringToDate(birthday);
//        System.out.println("dateConvertSdf(birthday)==========>" + date);
        String currentPage = req.getParameter("currentPage");
        String pageSize = req.getParameter("pageSize");
        //封装Student对象
        Student stu = new Student();
        stu.setNumber(number)
                .setName(name)
                .setBirthday(date)
                .setAddress(address);
        studentService.updateStu(stu);
        //重定向到分页查询功能
        try {
            resp.sendRedirect(req.getContextPath() + "/studentServlet?method=selectByPage&currentPage=" + currentPage + "&pageSize=" + pageSize);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * 添加学生方法
     *
     * @param req
     * @param resp
     */
    private void addStu(HttpServletRequest req, HttpServletResponse resp) {
        //获取请求参数
        String number = req.getParameter("number");
        String name = req.getParameter("name");
        String address = req.getParameter("address");
        String birthday = req.getParameter("birthday");
        Date date = DateUtil.StringToDate(birthday);
//        System.out.println("dateConvertSdf(birthday)==========>" + date);
        String currentPage = req.getParameter("currentPage");
        String pageSize = req.getParameter("pageSize");
        //封装Student对象
        Student stu = new Student();
        stu.setNumber(number)
                .setName(name)
                .setBirthday(date)
                .setAddress(address);
//        System.out.println("BeanUtils.populate(stu,map)=====>" + stu);
        studentService.addStu(stu);

        //重定向到分页查询功能
        try {
            resp.sendRedirect(req.getContextPath() + "/studentServlet?method=selectByPage&currentPage=" + currentPage + "&pageSize=" + pageSize);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /**
     * 分页查询功能
     *
     * @param req
     * @param resp
     */
    private void selectByPage(HttpServletRequest req, HttpServletResponse resp) {
        //获取请求参数
        String currentPage = req.getParameter("currentPage");
        String pageSize = req.getParameter("pageSize");

        //调用业务层的查询方法
        Page page = studentService.selectByPage(Integer.parseInt(currentPage), Integer.parseInt(pageSize));
        //封装PageInfo
        PageInfo info = new PageInfo(page);

        //将info转成json，响应给客户端
        try {
            String json = new ObjectMapper().writeValueAsString(info);
            resp.getWriter().write(json);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }


}
